package com.amado.designPattern.d05decorator.demo1;
import java.io.*;
public class FilterToLowerCase extends FilterInputStream{
 
    protected FilterToLowerCase(InputStream in) {
        super(in);
    }
    
    @Override
    public int read() throws IOException{
        int ch = super.read();
        if (ch >= 'A' && ch <= 'Z'){
            ch = Character.toLowerCase(ch);
        }
        return ch;
    }
 
    @Override
    public int read(byte[] b) throws IOException {
        int ch = super.read(b);
        for (int i=0;i<b.length;i++) {
            if (b[i] >= 'A' && b[i] <= 'Z'){
                b[i] = (byte)Character.toLowerCase(b[i]);
            }
        }
        return ch;
    }
    
    public static void main(String[] args) throws IOException {
    	FilterToLowerCase test = new FilterToLowerCase(new FileInputStream("D:\\test.txt"));
        int ch = 0;
        while ((ch = test.read()) != -1){
            System.out.print((char)ch);
        }
        test.close();
        /**一次性读取一个字符数组
        Test test = new Test(new FileInputStream(./data.txt"));
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = test.read(bytes))!= -1){
            String s = new String(bytes,0,len);
            System.out.print(s);
        }
        test.close();*/
    }
}
